package com.crdeng.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crdeng.business.entity.Consumer;
import com.crdeng.business.entity.OutStock;
import com.crdeng.business.mapper.ConsumerMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crdeng.business.service.ConsumerService;
import com.crdeng.business.service.OutStockService;
import com.crdeng.business.vo.ConsumerVO;
import com.crdeng.business.vo.SupplierQueryMap;
import com.crdeng.business.vo.SupplierVO;
import com.crdeng.handler.BusinessException;
import com.crdeng.response.ResultCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author crdeng
 * @since 2021-02-05
 */
@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, Consumer> implements ConsumerService {

    @Autowired
    private OutStockService outStockService;


    @Override
    public IPage<Consumer> findConsumer(Integer pageNun, Integer pageSize, SupplierQueryMap queryMap) {
        Page<Consumer> consumerPage = new Page<>(pageNun, pageSize);
        QueryWrapper<Consumer> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(queryMap.getAddress())){
            queryWrapper.like("address",queryMap.getAddress());
        }
        if (!StringUtils.isEmpty(queryMap.getContact())){
            queryWrapper.like("contact",queryMap.getContact());
        }
        if (!StringUtils.isEmpty(queryMap.getName())){
            queryWrapper.like("name",queryMap.getName());
        }

        return baseMapper.selectPage(consumerPage,queryWrapper);
    }

    @Override
    public void deleteSupplierById(Long id) {
        Consumer byId = getById(id);
        if (byId==null){throw new BusinessException(ResultCode.PARAM_NOT_VALID.getCode(),"暂查询不到此消费者信息"); }
        QueryWrapper<OutStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("consumer_id",id);
        List<OutStock> list = outStockService.list(queryWrapper);
        if (list.size()>=1){
            throw new BusinessException(ResultCode.COMMON_FAIL.getCode(),"存在出库单关联,无法自己删除");
        }
        removeById(id);

    }

    @Override
    public Consumer add(ConsumerVO consumerVO) {
        Date date = new Date();
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerVO,consumer);
        consumer.setCreateTime(date);
        consumer.setModifiedTime(date);
        boolean save = save(consumer);
        if (!save){
            throw  new BusinessException(ResultCode.COMMON_FAIL.getCode(),"添加消费方信息失败");
        }

        return consumer;
    }

    @Override
    public void edit(ConsumerVO consumerVO) {
        if (consumerVO.getId()==null){
            throw new BusinessException(ResultCode.PARAM_NOT_VALID.getCode(),"编辑ID不能为空");
        }
        Consumer byId = getById(consumerVO.getId());
        if (byId==null){
            throw new BusinessException(ResultCode.PARAM_NOT_VALID.getCode(),"查询不到此消费方信息");
        }
        consumerVO.setModifiedTime(new Date());
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerVO,consumer);
        updateById(consumer);
    }

}
